#!/bin/bash
###################################################################################
## run2_create_cohort_level_data - Program is the sam eas the run1 program. I have just split the programs being run. The programs run here 
##                                 Process the raw claims and create things like 30 day costs or comorbidities.                
##                  
##                  
###################################################################################

###################################################################################
## USER INSTRUCTIONS (TODOS)
##
## TODO1. Users need to make sure that they have properly defined the directories 
##    at the top of the dirInit.sas and dirInit.do programs
##
## TODO2. SET EMAILON to 1 or 0 depending on whether you want to receive an email on crash
## 
## TODO3. Next users need to ensure that parameters are defined correctly for cohrt they are looking 
##    define. The section has several variables, some of which for the different restrictions the 
##    programs place on the cohorts. Everything should be explained in this section. The bash prgm
##    passes all of these parameters to the sas prgms.
##    NOTE: users need to define the ICD9 OR HCPCS codes they are using in the 1PrimaryExtractionPrmgs/1A_FlagCandidateEvents UNDER
##           HCPCScohrtDef.sas or/and ICD9cohrtDef.sas files. The cohorts defined their should match the COHRT NAME as well.
##
## TODO4. Finally the user needs to make sure this programs is running all of the programs of interest. 
##    or comment out and/or add other programs for this master program to run
##
## TODO5. To run this bash scrip from the terminal, type in "bash runAllprgms.sh"
##
## TODO6. MAKE SURE YOU ONLY RUN PROGRAMS WHICH YOUR DUA COVERS!
##
## TODO7. RUN THIS PROGRAM FROM TERMINAL 'nohup bash runAllprgms.sh & '
###################################################################################

## IMPORT FUNCTION -'runcode'  WHICH IS USED TO RUN SAS/STATA PRGRMS AND AUTOMATE LOGS AND ERROR CHECKING
LOGNAMESH=`basename "$0"`
LOGNAME="${LOGNAMESH%.*}"

## IMPORT FUNCTION -'runcode'  WHICH IS USED TO RUN SAS/STATA PRGRMS AND AUTOMATE LOGS AND ERROR CHECKING
source SASmacros/runcode.sh $LOGNAME


##------------------------------------------------------------------------------
## TODO2: DO YOU WANT THE PROGRAM TO EMAIL YOU ON CRASH - SET TO 1 FOR YES
##----------------------------------------------------b-------------------------
## EMAIL WILL BE SENT TO YOUR NBER EMAIL
EMAILON=1
rm $LOGNAME.log -f
touch $LOGNAME.log
echo "RUNNING PROGRAM ON THIS SERVER:: `uname -n`" >$LOGNAME.log

##------------------------------------------------------------------------------
## TODO3: DEFINE VARIABLES TO BE PASSED ONTO SAS MACRO WITHIN EACH PROGRAMS
##------------------------------------------------------------------------------
##I have defined loops here - you should just need to put your percent and cohort names in their,
for p in  20      ; do
    ## ami hip hk stk chf pnu 
    for cohrt  in stk ; do
       ## PARAMETERS USED BY ALL FILES
             ## COHORT LABEL USED TO SAVE DATASETS - YOU CREATE THIS. 
             ##   SHOULD MATCH THAT FOUND IN 1PrimaryExtractionPrmgs/1A_FlagCandidateEvents UNDER
             ##   THE FOLLOWING FILES HCPCScohrtDef.sas or/and ICD9cohrtDef.sas 
        COHRT=$cohrt


        ##THE LAST YEAR OF DATA YOU WANT TO PROCESS
        MEDYEARIN=1999
	    #CARYEARIN=1999
        #OPYEARIN=2001
        #IPYEARIN=1999
	    
        YEAROUT=2015
             ##NOTE TO BE PLACED IN THE LOG FILE ARCHIVES
        NOTE=SHRUTHI_AMI_COMO_2
             ##SIZE OF FILES YOU ARE RUNNING
        PCT=$p
             ##WINDOW OF CLAIMS TO PULL BEFORE AND AFTER INDEX EVENT DATE- IN YEARS


###################################################################################
## STEP 2:: 2create_index_level_measures : GENERATE INDEX LEVEL STATISTICS
###################################################################################

#####################################################################################
#### COMPILE TOTAL REIMBURSEMENTS AT THE INDEX LEVEL - 30-DAY COSTS
#####################################################################################
# define X day cost you want to generate after index event, ie 30 creates cost occuring 30 days after index
	DAY1=30
	DAY2=90
	DAY3=365
	DAY4=
#dunmber of cost stats
	DNUM=3
##FIRST YEAR TO USE TO CREATE STATS
	YEARIN=$MEDYEARIN
        #runcode 2create_index_level_measures/xday_costs_raw CumulativeCostsPerIndx.sas cohrt=$COHRT,pct=$PCT,yearin=$YEARIN,yearout=$YEAROUT,NOTE=$NOTE,day1=$DAY1,day2=$DAY2,day3=$DAY3,day4=$DAY4,day5=$DAY5,day6=$DAY6,day7=$DAY7,dNum=$DNUM

        #runcode 2create_index_level_measures/xday_costs_raw CumulativeCostsPerIndx_last_year.sas cohrt=$COHRT,pct=$PCT,yearin=$YEARIN,yearout=$YEAROUT,NOTE=$NOTE,day1=$DAY1,day2=$DAY2,day3=$DAY3,day4=$DAY4,day5=$DAY5,day6=$DAY6,day7=$DAY7,dNum=$DNUM
# #####################################################################################



# #####################################################################################
# ## GENERATE HCC COMORBIDITY DUMMYS USING MEDPAR CLAIMS - LOOK BACK - AT THE INDX - use version 12 of CC
# #####################################################################################
 	runcode 2create_index_level_measures/last_yr_comorbid 0create_cc_icd9_crosswalk.do
 	runcode 2create_index_level_measures/last_yr_comorbid ExtComorbidCC.sas cohrt=$COHRT,pct=$PCT,NOTE=$NOTE
 	runcode 2create_index_level_measures/last_yr_comorbid check_icd9_padding.do $COHRT $PCT


#####################################################################################


 	if [[ "$p" -ne "100" ]]
	then
	    echo "cost utiliztion"
#####################################################################################
#### GENERATE COST UTILIZATION MEASURES - NOT USE OFTEN
#####################################################################################
	    ##    NOTE OP CU MEASURE START IN 2001 - HARD CODED TO START  IN 2001
	    ##IMPORT RVUs
	    #runcode 2create_index_level_measures/xday_cost_utiladj/carrierRVU importRVU.do
    	    #runcode 2create_index_level_measures/xday_cost_utiladj 2A_CallAllCostUtilizationPrgms.sas cohrt=$COHRT,pct=$PCT,yearin=$CARYEARIN,yearout=$YEAROUT,NOTE=$NOTE,clmDTA=$CONVERT2STATA
## 
###### SUM THOSE COSTS TO THE INDEX LEVEL
##            #define day costs intrested in created
 	    #DAY1=30
 	    #DAY2=90
 	    #DAY3=365
##            #dunmber of cost stats
 	    #DNUM=3
##            #limit to contiguous 48 states - suggested to keep this 1=yes 
 	    #CONT48=1
####FIRST YEAR TO USE TO CREATE STATS
 	    #YEARIN=$MEDYEARIN
 	    #runcode 2create_index_level_measures/xday_cost_utiladj 2B_GenerateIndxLevelsCostUtilStats.sas cohrt=$COHRT,pct=$PCT,yearin=$MEDYEARIN,yearout=$YEAROUT,NOTE=$NOTE,day1=$DAY1,day2=$DAY2,day3=$DAY3,day4=$DAY4,day5=$DAY5,day6=$DAY6,day7=$DAY7,dNum=$DNUM,contigious48=$CONT48
####---------------------------------------------------------------------------------
	fi          

## THESE ARE PROGRAMS WHICH ARE NOT OFTEN USED
####---------------------------------------------------------------------------------
## GENEARTE PROCEDURE DUMMY FOR AMI WHICH CROSS CHECK THE PROCEDURE PHYSICAIN 
## PROCEDURES WITH THE INSTITUTIONAL CLAIMS
## NEED TO DEFINE THE PROCEDURE CODE OF INTEREST - CURRENTLY ONLY SET UP FOR AMIs
#runcode 2create_index_level_measures/xday_flag_procedures Step1_tagProcs.sas cohrt=$COHRT,pct=$PCT,NOTE=$NOTE
#runcode 2create_index_level_measures/xday_flag_procedures Step2_matchProcs.sas cohrt=$COHRT,pct=$PCT,NOTE=$NOTE
####---------------------------------------------------------------------------------

####---------------------------------------------------------------------------------
## FLAG TRANSFERS IN THE MEDPAR CLAIMS FILES
##ICD9 CODE TO LOOK FOR -- USED FOR DEFINING TRANSFERS
##CODE=428
##runcode 2create_index_level_measures/transfers FlagTransfers.sas cohrt=$COHRT,pct=$PCT,code=$CODE
####---------------------------------------------------------------------------------




###################################################################################
## STEP 3:: 3FinalReportGenerationCALLSOFTWAREs : Write reports
###################################################################################
##DID YOU RUN PROCEDURE CODE - SET TO 1 for yes (DEFAULT IS U DIDNT)
PROCEDURE=0
##DID YOU RUN CU CODE - SET TO 1 for yes
#COSTUTILIZATION=0
#runcode 3merge_cohort_level_data/  combine_cohrt_level_data.do $COHRT $PCT $PROCEDURE $COSTUTILIZATION 


    done;
done;


echo "your program run has finished. cheers `uname -s`" |  mail -s "FINISHED RUN" `whoami`@nber.org 





